package com.qk.management.controller;

import com.qk.common.util.PageResult;
import com.qk.common.util.Result;
import com.qk.domain.clue.ClueDO;
import com.qk.domain.clue.ClueFollowDto;
import com.qk.dto.clue.ClueDto;
import com.qk.dto.clue.ClueListDto;
import com.qk.dto.clue.CluePoolDto;
import com.qk.entity.Clue;
import com.qk.management.anno.LogOperation;
import com.qk.management.constant.ClueStatusConstants;
import com.qk.management.service.ClueService;
import com.qk.vo.ClueVO;
import com.qk.vo.clue.CluePoolVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
@SuppressWarnings("all")
public class ClueController {
    @Autowired
    private ClueService clueService;

    @PutMapping
    public Result updateClueById(@RequestBody ClueFollowDto cfd) {
        log.info("跟进线索：{}", cfd);
            clueService.updateClueById(cfd);
            return Result.success();

    }

    @PostMapping
    @LogOperation
    public Result addClue(@RequestBody ClueDto clueDto) {
        clueService.addClue(clueDto);
        return Result.success();
    }

    @GetMapping
    public Result list(ClueListDto clueListDto) {
        PageResult<ClueDO> list = clueService.list(clueListDto);
        return Result.success(list);
    }

    @PutMapping("/assign/{clueId}/{userId}")
    @LogOperation
    public Result assign(@PathVariable("clueId") Integer clueId, @PathVariable("userId") Integer userId) {
        log.info("分配线索：{}", clueId);

        clueService.updateById(Clue.builder()
                .id(clueId)
                .userId(userId)
                .status(ClueStatusConstants.WAIT_FOLLOW_UP)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClueById(@PathVariable("id") Integer id) {
        ClueVO clueVO = clueService.getClueById(id);
        return Result.success(clueVO);
    }
    @PutMapping("/toBusiness/{id}")
    @LogOperation
    public Result toBusiness(@PathVariable("id") Integer id) {
        log.info("线索转商机：{}", id);
        clueService.toBusiness(id);
        return Result.success();
    }
    @PutMapping("/false/{id}")
    @LogOperation
    public Result fakeclue(@PathVariable("id") Integer id) {
        log.info("设置伪线索：{}", id);
        clueService.updateById(Clue.builder()
                .id(id)
                .status(ClueStatusConstants.FAKER_CLUE)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    @GetMapping("/pool")
    public Result pool(CluePoolDto cpd) {
        log.info("获取线索池",cpd);
        PageResult<CluePoolVo> pool = clueService.pool(cpd);
        return Result.success(pool);
    }
}
